import {Injectable} from '@angular/core';
import {RequestUtil} from "../../../../util/request-util";
import {FormUtil} from "../../../../util/form-util";
import {Observable} from "rxjs";
import {MsgResult} from "../../../../models/msg-result";
import {Instance} from "./instance";
import {API} from "../../../../constants/constants";
import {MsgAPI} from '../../msg-constants';

@Injectable()
export class InstanceService {

  constructor(private request: RequestUtil,
              private formUtil: FormUtil) {
  }

  //添加实例
  addInstance(instance: Instance,isGroup:string): Observable<MsgResult> {
    let params = "alarmName=" + instance.alarmName;
    params += "&content=" + instance.content;
    params += "&alarmId=" + instance.alarmId;
    params += "&code=" + instance.code;
    params += "&children=" + instance.children;
    params += "&isGroup=" + isGroup;
    params += "&receiver=" + instance.receiver;
    params += "&receiverName=" + instance.receiverName;
    params += "&sendMode=" + instance.sendMode;
    params += "&level=" + instance.level;
    if(instance.isReal == '0'){
      params +=  "&isReal="+instance.isReal;
    }else{
      params +=  "&isReal="+ '1';
    }
    params += "&type=" + instance.type;
    params += "&startTime=" + instance.startTime;
    params += "&endTime=" + instance.endTime;
    params += "&isUse=" + instance.isUse;
    params += "&ruleJson=";
    if('0'== isGroup){
      params +=  encodeURI(encodeURI(JSON.stringify(instance.alarmRules)));
    }
    // console.log(params);
    return this.request.post(MsgAPI.insertInstance, params);
  }

  //删除实例
  deleteInstance(id: string): Observable<MsgResult> {
    return this.request.delete(MsgAPI.deleteInstance + "&id=" + id);
  }

  //批量删除

  deleteInstances(ids: Array<String>): Observable<MsgResult> {
    let params = "idStrs=" + ids;
    return this.request.post(MsgAPI.delInstances, params);
  }

  //根据id获得通道
  getInstanceById(id: string): Observable<MsgResult> {
    return this.request.get(MsgAPI.getInstanceById + "&id=" + id);
  }

  //修改实例
  updateInstance(instance: Instance, isGroup: string): Observable<MsgResult> {
    let params = "alarmName=" + instance.alarmName;
    params += "&id=" + instance.id;
    params += "&content=" + instance.content;
    params += "&code=" + instance.code;
    params += "&alarmId=" + instance.alarmId;
    params += "&receiver=" + instance.receiver;
    params += "&receiverName=" + instance.receiverName;
    params += "&children=" + instance.children;
    params += "&isGroup=" + isGroup;
    params += "&sendMode=" + instance.sendMode;
    params += "&level=" + instance.level;
    if(instance.isReal == '0'){
      params +=  "&isReal="+instance.isReal;
    }else{
      params +=  "&isReal="+ '1';
    }
    params += "&type=" + instance.type;
    params += "&startTime=" + instance.startTime;
    params += "&endTime=" + instance.endTime;
    params += "&isUse=" + instance.isUse;
    params += "&ruleJson=" + encodeURI(encodeURI(JSON.stringify(instance.alarmRules)));
    return this.request.put(MsgAPI.updateInstance, params);
  }

  //无参获取所有记录
  getInstance(): Observable<MsgResult> {
    return this.request.get(MsgAPI.getInstance);
  }

  //获取行政区划树
  getRetrieveRegionsTree(): Observable<MsgResult> {
    return this.request.get(API.getRetrieveRegionsTree);
  }

  batchAddAlarmRule(alarmRule: string): Observable<MsgResult> {
    return this.request.post(MsgAPI.batchAddAlarmRule + "&ruleJson=" + alarmRule);
  }

}
